<?php
include_once "been/Filter.class.php";
class clientapiFilter extends Filter{
	
	protected $_openRS = array(
			'Index' => array('Test'),
			'Test' => array('Index','Index1','Upstring','Uplength'),
			'Item' => array('Getitemview'),
			'Video' => array('Playvideo'),
			'Common' => array('Upload','Notify'),
// 			'Homework'=>array('Uploadhomeworkimage'),
			'Account' => array('Login','Getcode','Checkcode','Getcodebyvoice','Resetpassword','Getdeviceinfo'),
	); //允许不用登陆访问的资源
	
	//属于账号的资源
	protected $_selfRS = array(	'Account' => array('Index','Login','Logout','Changepw','Myinfo'),
								'Index'	=>	array('Index','Main','Menu','Header') );
	
	//最终的资源列表
	protected $_RSList = array();
	
	public function doFilter(){
		$view = $this->getApp()->getView();
		if( $this->canViste( $this->getCName(), $this->getAName() ) ){
			$view->setStatus("0");
		}else{
			$session = $this->getApp()->loadUtilClass("SessionUtil");
			$userid = $session->get("session_userid");
			if($userid){
				$user=new user();
				$user_Phone_info = $user->getPhoneInfo($userid);
				if($user_Phone_info && $user_Phone_info['token'] !=  $session->get("token_id")){
					//$session->clear ();
					$view = $this->getApp()->getView();
					$view->setMsg("你的账号在其他设备登陆，如非本人操作，则密码可能已经泄露，建议修改密码。");
					$view->setState("-6");
					$view->setStatus("-1");
					$view->display("json");
					$message_list = $this->getApp()->loadConf("message");
					$message = new message();
					$message->addMessage(MessageCodeEnum::MESSAGE_SYS_OFFLINE,"null",$message_list['login_out'],0,$userid);
					return;
				}
				$userdata=$user->getInfoByid($userid);
				$this->getApp()->getView()->setStatus("0");
				if($userdata){
					//设置用户的信息
					$this->getApp()->putData('userinfo', $userdata );
					$view = $this->getApp()->getView();
					$view->setStatus("1");
					$this->_RSList = array_merge( $this->_RSList,$this->_selfRS );
				}else{
					$session->clear();
					$view = $this->getApp()->getView();
					$view->setState("0");
					$view->setMsg("非法的session信息");
					$view->setStatus("0");
					$view->display("json");
					exit();
				}
			}else{
				$view->setState("0");
				$view->setMsg("错误:请登录!");
				$view->setStatus("0");
				$view->display("json");
				return;
			}
		}
	
	}
	
	//检查是否可以访问
	public function canViste($cName,$aName){
		if( $this->isOpenRS($cName, $aName) ){
			return true;
		}else{
			return array_key_exists($cName,$this->_RSList) ? in_array( $aName, $this->_RSList[$cName]  ) : false;
		}
	}
	
	public function isOpenRS($cName,$aName){
		return  array_key_exists($cName, $this->_openRS) ? in_array($aName, $this->_openRS[$cName]) :false ;
	}

}

?>